#include"EquipmentController.h"
#include"../ServerMaster.h"
#include"../DBMaster.h"
#include"../dbconnect/Model.h"
#include"../dbconnect/DBDao.h"
#include"../Common.h"
#include<vector>

EquipmentController::EquipmentController(Object* master)
{
	m_master = master;
}

EquipmentController::~EquipmentController()
{
}

int EquipmentController::addEquipment(const int& userId, const int& base_id, const int& size)
{
	std::vector<int> ret;
	for (int i = 0; i < size; ++i)
	{
		int iRet = addEquipment(userId, base_id);
		if (iRet > 0)
		{
			ret.push_back(iRet);
		}
	}
	return 1;
}

int EquipmentController::addEquipment(const int& userId, const int& base_id)
{
	auto master = dynamic_cast<ServerMaster*>(m_master);
	int newId = master->getDBMaster()->getBlobalId(GLOBAL_EQUIPMENT);
	if (newId == 0)
	{
		return 0;
	}
	DBModel::Equipment equip;
	equip.id = newId;
	equip.userId = userId;
	equip.base_id = base_id;
	equip.level = 1;
	equip.rank_lv = 1;
	DBDao<DBModel::Equipment> dao(master->getDBMaster()->getSqlConnnect());
	dao.setModel(equip);
	if (dao.insertModel())
	{
		return newId;
	}
	else
	{
		return 0;
	}
}

int EquipmentController::upgradeEquipment(const int& id, const int& level)
{
	auto master = dynamic_cast<ServerMaster*>(m_master);
	DBModel::Equipment equip;
	equip.id = id;
	DBDao<DBModel::Equipment> dao(master->getDBMaster()->getSqlConnnect());
	dao.setModel(equip);
	auto list = dao.queryModel();
	if (list.size() > 0)
	{
		auto item = list[0];
		item.level += level;
		dao.setModel(item);
		if (dao.updateModel("id", id))
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return 0;
	}
}

int EquipmentController::rankUpEquipment(const int& id, const int& level)
{
	auto master = dynamic_cast<ServerMaster*>(m_master);
	DBModel::Equipment equip;
	equip.id = id;
	DBDao<DBModel::Equipment> dao(master->getDBMaster()->getSqlConnnect());
	dao.setModel(equip);
	auto list = dao.queryModel();
	if (list.size() > 0)
	{
		auto item = list[0];
		item.rank_lv += level;
		dao.setModel(item);
		if (dao.updateModel("id", id))
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return 0;
	}
}
